package com.aacar.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aacar.constant.ConsFiled;
import com.aacar.dao.PersonalDao;
import com.aacar.dto.WashCarDto;
import com.aacar.dto.WashCarPageDto;
import com.aacar.entity.WashCarAddr;
import com.aacar.entity.WashCarPage;
import com.aacar.service.PersonalService;
import com.aacar.vo.CommonVo;
import com.aacar.vo.WashCarVo;
/**
 * 增加洗车地址
 * @author wangh
 *
 */
@Repository("personalService")
public class PersonalServiceImpl implements PersonalService{
	
	@Autowired
	private PersonalDao personalDao;
	
	@Override
	public CommonVo managePersonalWashCar(WashCarDto washCarDto) {
		// TODO Auto-generated method stub
		return null;
	}
	
	//新增
	public CommonVo addPersonalWashCarRecord(WashCarDto washCarDto) {
		CommonVo cmv = new CommonVo();
		WashCarAddr wad = new WashCarAddr();
		wad.setUserId(washCarDto.getUserId());//用户id
		wad.setUserName(washCarDto.getUserName());//用户名字
		wad.setPhoneNo(washCarDto.getPhoneNo());//用户电话号码
		wad.setProvinceCode(washCarDto.getProvinceCode());//省代码
		wad.setProvinceName(washCarDto.getProvinceName());//省名称
		wad.setCityCode(washCarDto.getCityCode());//城市号码
		wad.setCityName(washCarDto.getCityName());//城市名称
		wad.setAreaCode(washCarDto.getAreaCode());//区域号码
		wad.setAreaName(washCarDto.getAreaName());//区域名称
		wad.setDetailAddress(washCarDto.getDetailAddress());//详细地址
		//判断下 如果没有默认地址就
		WashCarPage washCarPage = new WashCarPage();
		washCarPage.setUserId(washCarDto.getUserId());
		WashCarVo wcv = this.personalDao.queryCarAdrress(washCarPage);
		if(wcv == null) {
			wad.setIsDefault(washCarDto.getIsDefault());//是否默认地址
		}else {
			WashCarAddr wcd = new WashCarAddr();
			wcd.setUserId(washCarDto.getUserId());
			//更新地址
			this.personalDao.updateCarAddressByUserId(wcd);
			wad.setIsDefault(washCarDto.getIsDefault());//是否默认地址
		}
		wad.setIsValid("1");//有效
		wad.setAddrType(washCarDto.getAddrType());//标签
		wad.setRoomCode(washCarDto.getRoomCode());//小区code
		wad.setRoomName(washCarDto.getRoomName());//小区名字
		wad.setContactName(washCarDto.getContactName());//联系人姓名
		wad.setContactNo(washCarDto.getContactNo());//联系人号码
		int result = this.personalDao.addWashCarAddr(wad);
		if(result > 0) {
			cmv.setSuccess(true);
			cmv.setInfo(ConsFiled.operater_success);//操作成功
		}else {
			cmv.setSuccess(false);
			cmv.setInfo(ConsFiled.operater_error);//操作失败
		}
		return cmv;
	}
	
	//修改
	public CommonVo updatePersonalWashCarRecord(WashCarDto washCarDto) {
		CommonVo cmv = new CommonVo();
		WashCarAddr wad = new WashCarAddr();
		wad.setPid(washCarDto.getPid());
		wad.setUserName(washCarDto.getUserName());//用户名字
		wad.setPhoneNo(washCarDto.getPhoneNo());//用户电话号码
		wad.setProvinceCode(washCarDto.getProvinceCode());//省代码
		wad.setProvinceName(washCarDto.getProvinceName());//省名称
		wad.setCityCode(washCarDto.getCityCode());//城市号码
		wad.setCityName(washCarDto.getCityName());//城市名称
		wad.setAreaCode(washCarDto.getAreaCode());//区域号码
		wad.setAreaName(washCarDto.getAreaName());//区域名称
		wad.setDetailAddress(washCarDto.getDetailAddress());//详细地址
		wad.setIsDefault(washCarDto.getIsDefault());//是否默认地址
		wad.setIsValid(washCarDto.getIsValid());//是否有效
		wad.setAddrType(washCarDto.getAddrType());//标签
		wad.setContactName(washCarDto.getContactName());//联系人
		wad.setContactNo(washCarDto.getContactNo());//联系号码
		wad.setRoomCode(washCarDto.getRoomCode());//小区编号
		wad.setRoomName(washCarDto.getRoomName());//小区名字
		//删除
		if(washCarDto.getOperateFlag().equals("3")) {
			wad.setIsValid("0");
		}
		if(washCarDto.getOperateFlag().equals("2")) {
			wad.setUserId(washCarDto.getUserId());
			this.personalDao.updateCarAddressByUserId(wad);
		}
		//根据用户id查询默认值
		int result = this.personalDao.updatePersonalWashCarRecord(wad);
		if(result > 0) {
			cmv.setSuccess(true);
			cmv.setInfo(ConsFiled.operater_success);//操作成功
		}else {
			cmv.setSuccess(false);
			cmv.setInfo(ConsFiled.operater_error);//操作失败
		}
		return cmv;
	}

	public List<WashCarVo> queryCarAddressList(WashCarPageDto washCarPageDto) {
		WashCarPage washCarPage = new WashCarPage();
		washCarPage.setUserId(washCarPageDto.getUserId());
		int pageNo = washCarPageDto.getPageNo();
		int pageSize = washCarPageDto.getPageSize();
		pageNo = pageNo*pageSize;
		washCarPage.setPageNo(pageNo);
		washCarPage.setPageSize(pageSize);
		return this.personalDao.queryCarAddressList(washCarPage);
	}

	@Override
	public int queryCarAddressCount(WashCarPageDto washCarPageDto) {
		WashCarPage washCarPage = new WashCarPage();
		washCarPage.setUserId(washCarPageDto.getUserId());
		return this.personalDao.queryCarAddressCount(washCarPage);
	}

}
